#include <stdio.h>
struct User
{
	int id;
	char name[20];
};
int main(int argc,char *argv[])
{
	if(argc < 2)
	{
		printf("parameter error\n");
		return 1;
	}
	FILE *fp1 = fopen("/etc/passwd","r");
	FILE *fp2 = fopen(argv[1],"wb");
	if(fp1 == NULL || fp2 == NULL)
	{
		printf("parameter error\n");
		return 1;
	}
	struct User user;
	while(EOF != fscanf(fp1,"%[^:]:%*[^:]:%d:%*[^\n]\n",user.name,&user.id))
	{
		if(EOF == fwrite(&user,sizeof(struct User),1,fp2))
		{
			printf("fail to write file\n");
			return 1;
		}
		//printf("uid = %i;name = %s\n",user.id,user.name);
	}
	fclose(fp1);
	fclose(fp2);
	fp2 = fopen(argv[1],"rb");
	/* 利用返回值判断是否到达文件尾
	while(0 != fread(&user,sizeof(struct User),1,fp2))
	{
		printf("uid = %i;name = %s\n",user.id,user.name);
	}
	*/
	fread(&user,sizeof(struct User),1,fp2);
	// 使用feof判断是否是文件结尾
	while(!feof(fp2))
	{
		printf("uid = %i;name = %s\n",user.id,user.name);
		fread(&user,sizeof(struct User),1,fp2);
	}
	
	return 0;
}
